home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOPOLY.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-26  |  6.7 KB  |  234 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Polygon"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. ' >> Best viewed in Full Module view. <<
  12. '
  13. ' Storage for debug ID number.
  14. Private mlngDebugID As Long
  15. Implements IDebug
  16.  
  17. ' Polygon is a VERY rudimentary class of
  18. ' -------       polygon objects.  It only
  19. '   allows polygons to be created (with
  20. '   the SetPoints method); there's no way
  21. '   to modify the points once they're set.
  22. '   (Of course, you can call SetPoints
  23. '   again, to reset the entire polygon.)
  24.  
  25. Private Type POLYPOINT  ' prefix pyp
  26.     X As Single
  27.     Y As Single
  28.     Angle As Single
  29. End Type
  30.  
  31. ' Polygon defaults to one point, at (0,0).
  32. Private mapyp() As POLYPOINT
  33.  
  34. ' Storage for read-only Normalized property.
  35. Private mblnNormalized As Boolean
  36.  
  37. ' Storage for Color property.
  38. Private mrgbColor As Long
  39.  
  40. ' IShape is the interface that's used to
  41. ' ------    display the polygon.  It also
  42. '   has a TimeTest method that's used to
  43. '   compare early and late binding call
  44. '   overhead.
  45. Implements IShape
  46.  
  47. ' -------------------------------------
  48. ' This marks the beginning of the
  49. '   implementation of the IShape
  50. '   interface.
  51.  
  52. ' IShape.DrawToPictureBox is called to
  53. ' ====== ----------------   draw a shape,
  54. '   so each class of shape must supply
  55. '   its own implementation.
  56. '
  57. Private Sub IShape_DrawToPictureBox(ByVal pb As PictureBox)
  58.     Dim sngXLast As Single
  59.     Dim sngYLast As Single
  60.     Dim sngX As Single
  61.     Dim sngY As Single
  62.     Dim intMax As Integer
  63.     Dim intCt As Integer
  64.     
  65.     intMax = UBound(mapyp)
  66.     sngX = mapyp(intMax).X
  67.     sngY = mapyp(intMax).Y
  68.     If intMax = 0 Then
  69.         pb.PSet (sngX, sngY), mrgbColor
  70.     Else
  71.         For intCt = 0 To intMax
  72.             sngXLast = sngX
  73.             sngYLast = sngY
  74.             sngX = mapyp(intCt).X
  75.             sngY = mapyp(intCt).Y
  76.             pb.Line (sngXLast, sngYLast)-(sngX, sngY), mrgbColor
  77.         Next
  78.     End If
  79. End Sub
  80.  
  81. ' IShape.TimeTest method is used to show
  82. ' ====== --------       the reduced call
  83. '   overhead of a method called on an
  84. '   interface that several classes
  85. '   implement -- as opposed to calling
  86. '   a similar method on the classes'
  87. '   default interfaces.
  88. '
  89. Private Sub IShape_TimeTest()
  90. End Sub
  91.  
  92. ' --------------------------------------
  93. ' This is the beginning of the Polygon
  94. '   class's default interface (Public
  95. '   properties and methods).  This is
  96. '   the Polygon interface that Triangle
  97. '   and Rectangle implement.
  98.  
  99. ' Color property.
  100. ' -----
  101. '
  102. Public Property Get Color() As Long
  103.     Color = mrgbColor
  104. End Property
  105. '
  106. Public Property Let Color(ByVal rgb As Long)
  107.     If 0 <> (rgb And &HFF000000) Then
  108.         Err.Raise vbObjectError + 2080, , _
  109.             "Invalid color value for Polygon."
  110.         Exit Property
  111.     End If
  112.     mrgbColor = rgb
  113. End Property
  114.  
  115. ' TimeTest method takes no arguments,
  116. ' --------      does nothing, and
  117. '   immediately returns.  It's used to
  118. '   illustrate the call overhead for
  119. '   late binding, as opposed to the
  120. '   early binding provided by calling
  121. '   TimeTest on the IShape interface.
  122. '
  123. ' You might think we would make TimeTest
  124. '   a Friend property, like DebugID, to
  125. '   save Triangle and Rectangle -- which
  126. '   implement Polygon's interface -- from
  127. '   having to implement Polygon_TimeTest.
  128. '   (Friend properties and methods are
  129. '   NOT part of a class's interface.)
  130. '   The reason we can't do this is that
  131. '   TimeTest must be called LATE bound
  132. '   for the demo -- but Friend properties
  133. '   and methods must always be called
  134. '   EARLY bound.
  135. Public Sub TimeTest()
  136. End Sub
  137.  
  138. ' GetPoint sets two ByRef Singles to
  139. ' --------      the X and Y values for
  140. '   the requested point.  (If Polygon's
  141. '   interface wasn't being implemented
  142. '   by Triangle and Rectangle, GetPoint
  143. '   could be declared Friend, and could
  144. '   return a POLYPOINT -- which would
  145. '   have to be declared Public in a
  146. '   standard module in that case;
  147. '   however, Friend members are not part
  148. '   of a class's interface, so making
  149. '   GetPoint a Friend would prevent
  150. '   Triangle and Rectangle from
  151. '   implementing an early-bound
  152. '   Polygon_GetPoint.)
  153. '
  154. Public Sub GetPoint(ByVal intPoint As Integer, _
  155.         ByRef X As Single, ByRef Y As Single)
  156.     X = mapyp(intPoint).X
  157.     Y = mapyp(intPoint).Y
  158. End Sub
  159.  
  160. ' GetPointCount returns the number of
  161. ' -------------     points in the Polygon.
  162. '
  163. Public Property Get GetPointCount() As Integer
  164.     GetPointCount = UBound(mapyp) + 1
  165. End Property
  166.  
  167. ' SetPoints accepts a zero-based array
  168. ' ---------     of Singles, the even-numbered
  169. '   elements (0, 2, etc.) being the X
  170. '   values, and the odd-numbered elements
  171. '   being the Y values of the points.
  172. '
  173. Public Sub SetPoints(asngPoints() As Single)
  174.     Dim blnBadArray As Boolean
  175.     Dim intMax As Integer
  176.     Dim intPoint As Integer
  177.     
  178.     On Error Resume Next
  179.     If LBound(asngPoints) <> 0 Then blnBadArray = True
  180.     intMax = UBound(asngPoints)
  181.     ' The upper bound of a zero-based
  182.     '   array must be an odd number --
  183.     '   validate this.
  184.     If (intMax / 2#) = (intMax \ 2) Then blnBadArray = True
  185.     ' If an error occurs in the UBound
  186.     '   function, declare array invalid.
  187.     If Err.Number <> 0 Then blnBadArray = True
  188.     If blnBadArray Then
  189.         Err.Raise vbObjectError + 2081, , _
  190.             "SetPoints must receive a zero-based, one-dimensional array with an even number of elements, the odd entries being X values and the even entries Y values."
  191.         Exit Sub
  192.     End If
  193.     ' Convert the maximum index of the input
  194.     '   array to the maximum index of the
  195.     '   internal array of the Polygon.
  196.     intMax = intMax \ 2
  197.     ReDim mapyp(0 To intMax)
  198.     ' Read in the point values.
  199.     For intPoint = 0 To intMax
  200.         mapyp(intPoint).X = asngPoints(intPoint * 2)
  201.         mapyp(intPoint).Y = asngPoints(intPoint * 2 + 1)
  202.     Next
  203. End Sub
  204.  
  205. ' --------------------------------------
  206. ' This is the beginning of the Polygon's
  207. '   private procedures (helper procedures
  208. '   and event procedures).
  209.  
  210. Private Sub Class_Initialize()
  211.     ' Debug code.
  212.     mlngDebugID = DebugInit(Me)
  213.     '
  214.     ' Polygon defaults to a point.
  215.     ReDim mapyp(0 To 0)
  216. End Sub
  217.  
  218. Private Sub Class_Terminate()
  219.     DebugTerm Me
  220. End Sub
  221.  
  222. ' -------- IDebug Implementation --------
  223. '
  224. ' IDebug.DebugID gives you a way to tell
  225. ' ====== -------    objects apart.  It's
  226. '   required by the DebugInit, DebugTerm,
  227. '   and DebugShow debugging procedures
  228. '   declared in modFriend.
  229. '
  230. Private Property Get IDebug_DebugID() As Long
  231.     IDebug_DebugID = mlngDebugID
  232. End Property
  233.  
  234.